package com.firefighting.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.firefighting.dao.BuildingMapper;
import com.firefighting.dao.ContractMapper;
import com.firefighting.entity.Building;
import com.firefighting.entity.Contract;

/**
 * @author Liuxc
 * 
 * function 合同建筑物相关接口
 * 
 * */
@Service
public class BuildingService {
	
	@Autowired
	private BuildingMapper buildingMapper;
	@Autowired
	private ContractMapper contractMapper;
/**
 * @author Liuxc
 * @function 更新建筑物
 * 
 * */
	@Transactional(rollbackFor = Exception.class)
	public void updateBuilding(Building building) {
		// TODO Auto-generated method stub
		int contractId = building.getContractId();
        buildingMapper.updateByPrimaryKeySelective(building);
        int totalArea = buildingMapper.sumArea(contractId);
        Contract  contract = new Contract(); 
        contract.setId(contractId);
        contract.setBuildingArea(totalArea);
        contractMapper.updateByPrimaryKeySelective(contract);
	}
	/**
	 * @author Liuxc
	 * @function 增加建筑物
	 * 
	 * */
	@Transactional(rollbackFor = Exception.class)
	public void addBuilding(Building building) {
		// TODO Auto-generated method stub
		int contractId = building.getContractId();
		buildingMapper.insert(building);
		int totalArea = buildingMapper.sumArea(contractId);
        Contract  contract = new Contract(); 
        contract.setId(contractId);
        contract.setBuildingArea(totalArea);
        contractMapper.updateByPrimaryKeySelective(contract);
		
	}
	/**
	 * @author Liuxc
	 * @function 删除建筑物
	 * 
	 * */
	@Transactional(rollbackFor = Exception.class)
	public void deleteById(int id) {
		// TODO Auto-generated method stub
        Building building = buildingMapper.selectByPrimaryKey(id);
        int contractId = building.getContractId();
		buildingMapper.deleteByPrimaryKey(id);
		int totalArea = buildingMapper.sumArea(contractId);
        Contract  contract = new Contract(); 
        contract.setId(contractId);
        contract.setBuildingArea(totalArea);
        contractMapper.updateByPrimaryKeySelective(contract);
		
	}
	
	/**
	 * @author Liuxc
	 * 根据id查询building
	 * */
	public Building selectBuildingById(Integer buildingId) {
		// TODO Auto-generated method stub
		return buildingMapper.selectByPrimaryKey(buildingId);
	}
	/**
	 * @author Liuxc
	 * 查询合同的建筑物列表
	 * */
	public List<HashMap<String, Object>> selectBuildingList(Integer contractId) {
		// TODO Auto-generated method stub
		return buildingMapper.selectBuildingList(contractId);
	}
	
	/** zhyan 查询所有建筑的名称 */
	@SuppressWarnings("rawtypes")
	public List<HashMap> queryAllBuilding()
	{
		return buildingMapper.selectAllBuilding();
	}
	public int getNumberOfContract(Integer contractId) {
		// TODO Auto-generated method stub
		return buildingMapper.getNumberOfContract(contractId);
	}
}
